package org.kjtc.balanceController;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.kjtc.balanceEntity.ChargingRecord;
import org.kjtc.balanceEntity.ReviewRecord;
import org.kjtc.balanceService.ReviewRecordService;
import org.kjtc.controller.BaseController;
import org.kjtc.entity.LineInfo;
import org.kjtc.entity.UserInfo;
import org.kjtc.service.UserInfoService;
import org.kjtc.util.Const;
import org.kjtc.util.ExcelUtil;
import org.kjtc.util.FileDownload;
import org.kjtc.util.FileUpload;
import org.kjtc.util.Tools;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class ReviewRecordController extends BaseController{

	@Resource(name = "reviewRecordService")
	private ReviewRecordService reviewRecordService;
	
	@Resource(name = "userInfoService")
	private UserInfoService userInfoService;
	
	@RequestMapping(value = "/searchReviewRecord")
	@ResponseBody
	public Map<String, Object> searchReviewRecord(ReviewRecord reviewRecord, String lineIDList, String chargingStationIDList){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核记录查询开始");
		if(lineIDList != null && !"".equals(lineIDList)) {
			String[] lineID = lineIDList.split(",");
			String[] chargingStationID = chargingStationIDList.split(",");
			ChargingRecord chargingRecord = new ChargingRecord();
			List<ChargingRecord> list = new ArrayList<ChargingRecord>();
			for(int i = 0; i < lineID.length; i++){
				chargingRecord.setChargingStationID(chargingStationID[i]);
				chargingRecord.setLineID(lineID[i]);
				list.add(chargingRecord);
			}
			reviewRecord.setChargingRecordList(list);
		}
		List<ReviewRecord> list = null;
		List<ReviewRecord> lineIDLists = null;
		try {
			list = reviewRecordService.getReviewRecord(reviewRecord);
			for(ReviewRecord reviewRecords: list){
				if(lineIDList != null && !"".equals(lineIDList)) {
					lineIDList = lineIDList.concat("," + reviewRecords.getLineID());
				} else {
					lineIDList = reviewRecords.getLineID();
				}
			}
			reviewRecord.setLineIDList(lineIDList.split(","));
			lineIDLists = reviewRecordService.getLineIDList(reviewRecord);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核记录查询异常" + e.getMessage());
			return map;
		}
		map.put("reviewRecordList", list);
		map.put("reviewLineIDList", lineIDLists);
		reviewRecord = null;
		return map;
	}
	
	@RequestMapping(value = "/searchLineRecordDetail")
	@ResponseBody
	public Map<String, Object> searchLineRecordDetail(ChargingRecord chargingRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "线路详细记录查询");
		
		if(chargingRecord.getSubBranchCompanyID().equals("") || chargingRecord.getSubBranchCompanyID() == null){
			map.put("result", "SubBranchCompanyIDNull");
		} else if(chargingRecord.getTeamID().equals("") || chargingRecord.getTeamID() == null) {
			map.put("result", "TeamIDNull");
		} else if(chargingRecord.getLineID().equals("") || chargingRecord.getLineID() == null) {
			map.put("result", "LineIDNull");
		} else if(chargingRecord.getStartChargingDate().equals("") || chargingRecord.getStartChargingDate() == null) {
			map.put("result", "StartChargingDateNull");
		} else if(chargingRecord.getEndChargingDate().equals("") || chargingRecord.getEndChargingDate() == null) {
			map.put("result", "getEndChargingDateNull");
		} else {
			List<ChargingRecord> list = null;
			this.getSession().setAttribute("searchChargingRecord", chargingRecord);
			try {
				list = reviewRecordService.getLineRecordDetail(chargingRecord);
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "线路详细记录查询异常" + e.getMessage());
				return map;
			}
			map.put("lineRecordDetailList", list);
		}
		chargingRecord = null;
		return map;
	}
	
	@RequestMapping(value = "/searchReviewedRecord")
	@ResponseBody
	public Map<String, Object> getReviewedRecord(ReviewRecord reviewRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "已审核记录查询");
		boolean bool = true;
		if(this.getUesrInfo().getRoleID().equals("LR_USER_SHRO")) {
			if(reviewRecord.getTeamID() == null || reviewRecord.getTeamID().equals("")) {
				map.put("result", "TeamIDNull");
				bool = false;
			} else {
				String [] teamIDList = reviewRecord.getTeamID().split(",");
				reviewRecord.setTeamIDList(teamIDList);
				reviewRecord.setTeamID("");
			}
		}
		if(bool) {
			List<ReviewRecord> list = null;
			this.getSession().setAttribute("searchReviewRecord", reviewRecord);
			try {
				list = reviewRecordService.getReviewedRecord(reviewRecord);
				for(ReviewRecord reviewedRecord : list) {
					String filePath = null;
					File file = null;
					filePath = Const.BALANCE_PATH.concat("reviewfile");
					String [] reviewDateArr = reviewedRecord.getReviewDate().split("[^\\d]");
					filePath = filePath.concat(File.separator).concat(reviewDateArr[0]).concat(File.separator).concat(reviewDateArr[1]).concat(File.separator).concat(reviewDateArr[2]).concat(File.separator).concat(reviewedRecord.getChargingStationID());
					file = new File(filePath);
					if(file.exists()) {
						File[] reviewFiles = file.listFiles();
						for (File reviewFile : reviewFiles) {
							String reviewFileName = reviewFile.getName();
							if (reviewedRecord.getLineID().equals(reviewFileName.split("\\.")[0])) {
								reviewedRecord.setReviewFile(reviewFileName);
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "已审核记录查询异常" + e.getMessage());
				return map;
			}
			map.put("reviewedRecordList", list);
			reviewRecord = null;
		}
		return map;
	}
	
	@RequestMapping(value = "/searchLineKWHEntry")
	@ResponseBody
	public Map<String, Object> searchLineKWHEntry(ReviewRecord reviewRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核记录录入查询开始");
		List<ReviewRecord> list = null;
		try {
			list = reviewRecordService.searchLineKWHEntry(reviewRecord);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核记录录入查询异常" + e.getMessage());
			return map;
		}
		map.put("lineKWHEntryList", list);
		reviewRecord = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/saveManualReview")
	public Map<String, Object> saveManualReview(ReviewRecord reviewRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加审核记录");
		
		reviewRecord.setCreateUser(this.getUesrInfo().getUserID());
		reviewRecord.setUpdateUser(this.getUesrInfo().getUserID());
		int count = 0;
		try {
			count = reviewRecordService.saveManualReview(reviewRecord);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("Reviewed".equals(error[3].trim())) {
				map.put("result", "ReviewedExist");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加审核记录异常:" + e.getMessage());
		}
        
        reviewRecord = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/saveLineKWHEntry")
	public Map<String, Object> saveLineKWHEntry(ReviewRecord reviewRecord, String reviewDateList, String chargingKWHList){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加审核记录");
		String[] reviewDates = reviewDateList.split(",");
		String[] chargingKWHs = chargingKWHList.split(",");
		ReviewRecord reviewRecord1 = null;
		List<ReviewRecord> importReviewRecordList = new ArrayList<ReviewRecord>();
		for(int i = 0; i < reviewDates.length; i++) {
			reviewRecord1 = new ReviewRecord();
			reviewRecord1.setReviewDate(reviewDates[i]);
			reviewRecord1.setTotalChargingKWH(0);
			reviewRecord1.setSumChargingKWH(Double.valueOf(chargingKWHs[i]));
			reviewRecord1.setChargingCount(0);
			reviewRecord1.setChargingStationName(reviewRecord.getChargingStationName());
			reviewRecord1.setSubBranchCompanyName(reviewRecord.getSubBranchCompanyName());
			reviewRecord1.setTeamName(reviewRecord.getTeamName());
			reviewRecord1.setLineName(reviewRecord.getLineName());
			reviewRecord1.setReviewState(2);
			reviewRecord1.setReviewWay("entering");
			reviewRecord1.setReviewPerson(this.getUesrInfo().getUserName());
			importReviewRecordList.add(reviewRecord1);
			reviewRecord1 = null;
		}
		int count = 0;
		try {
			count = reviewRecordService.saveReviewRecord(importReviewRecordList, this.getUesrInfo().getUserID());
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("Reviewed".equals(error[3].trim())) {
				map.put("result", "ReviewedExist");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加审核记录异常:" + e.getMessage());
		}
        
        reviewRecord = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateManualReview")
	public Map<String, Object> updateManualReview(ReviewRecord reviewRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加审核记录");
		
		reviewRecord.setUpdateUser(this.getUesrInfo().getUserID());
		int count = 0;
		try {
			count = reviewRecordService.updateManualReview(reviewRecord);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加审核记录异常:" + e.getMessage());
		}
        
        reviewRecord = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateReviewState")
	public Map<String, Object> updateReviewState(ReviewRecord reviewRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核线路记录");
		
		if(reviewRecord.getSubBranchCompanyID().equals("") || reviewRecord.getSubBranchCompanyID() == null){
			map.put("result", "SubBranchCompanyIDNull");
		} else if(reviewRecord.getTeamID().equals("") || reviewRecord.getTeamID() == null) {
			map.put("result", "TeamIDNull");
		} else if(reviewRecord.getLineID().equals("") || reviewRecord.getLineID() == null) {
			map.put("result", "LineIDNull");
		} else if(reviewRecord.getStartChargingDate().equals("") || reviewRecord.getStartChargingDate() == null) {
			map.put("result", "StartChargingDateNull");
		} else if(reviewRecord.getEndChargingDate().equals("") || reviewRecord.getEndChargingDate() == null) {
			map.put("result", "EndChargingDateNull");
		} else {
			int count = 0;
			try {
				count = reviewRecordService.updateReviewState(reviewRecord);
				if (count > 0) {
					map.put("result", "Success");
				} else {
					map.put("result", "NotFound");
				}
			} catch (Exception e) {
				e.printStackTrace();
				map.put("result", "Error");
				logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核线路记录异常:" + e.getMessage());
			}
		}
        reviewRecord = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateRecordReviewState")
	public Map<String, Object> updateRecordReviewState(ReviewRecord reviewRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核记录确认");
		
		if(reviewRecord.getSubBranchCompanyID().equals("") || reviewRecord.getSubBranchCompanyID() == null){
			map.put("result", "SubBranchCompanyIDNull");
		} else if(reviewRecord.getTeamID().equals("") || reviewRecord.getTeamID() == null) {
			map.put("result", "TeamIDNull");
		} else if(reviewRecord.getLineID().equals("") || reviewRecord.getLineID() == null) {
			map.put("result", "LineIDNull");
		} else if(reviewRecord.getStartChargingDate().equals("") || reviewRecord.getStartChargingDate() == null) {
			map.put("result", "StartChargingDateNull");
		} else if(reviewRecord.getEndChargingDate().equals("") || reviewRecord.getEndChargingDate() == null) {
			map.put("result", "getEndChargingDateNull");
		} else {
			int count = 0;
			try {
				count = reviewRecordService.updateRecordReviewState(reviewRecord);
				if (count > 0) {
					map.put("result", "Success");
				} else {
					map.put("result", "NotFound");
				}
			} catch (Exception e) {
				e.printStackTrace();
				map.put("result", "Error");
				logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核记录确认异常:" + e.getMessage());
			}
		}
        reviewRecord = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteReviewRecord")
	public Map<String, Object> deleteReviewRecord(ReviewRecord reviewRecord) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "重新审核记录");
		
		int count1 = 0;
		try {
			count1 = reviewRecordService.deleteReviewRecord(reviewRecord);
			if (count1 > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "重新审核记录异常:" + e.getMessage());
		}
        reviewRecord = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteReviewState")
	public Map<String, Object> deleteRecordReviewState(ReviewRecord reviewRecord) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "重新审核记录");
		
		if(reviewRecord.getLineID() == null || reviewRecord.getLineID().equals("")) {
			map.put("result", "LineIDNull");
		} else if(reviewRecord.getStartChargingDate() == null || reviewRecord.getStartChargingDate().equals("")) {
			map.put("result", "StartChargingDateNull");
		} else if(reviewRecord.getEndChargingDate() == null || reviewRecord.getEndChargingDate().equals("")) {
			map.put("result", "getEndChargingDateNull");
		} else {
			int count1 = 0;
			int count2 = 0;
			try {
				count1 = reviewRecordService.deleteReviewRecord(reviewRecord);
				count2 = reviewRecordService.deleteRecordReviewState(reviewRecord);
				if(reviewRecord.getReviewFile() != null && !reviewRecord.getReviewFile().equals("")) {
					String [] reviewDateArr = reviewRecord.getReviewDate().split("[^\\d]");
					String path = Const.BALANCE_PATH.concat("reviewfile").concat(File.separator).concat(reviewDateArr[0]).concat(File.separator).concat(reviewDateArr[1]).concat(File.separator).concat(reviewDateArr[2]).concat(File.separator).concat(reviewRecord.getChargingStationID()).concat(File.separator);
					path = path + reviewRecord.getReviewFile();
					File file = new File(path);
					if (file.exists()) {
						file.delete();
					}
				}
				if (count1 > 0 && count2 >0) {
					map.put("result", "Success");
				} else {
					map.put("result", "NotFound");
				}
			} catch (Exception e) {
				e.printStackTrace();
				map.put("result", "Error");
				logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "重新审核记录异常:" + e.getMessage());
			}
		}
        reviewRecord = null;
		return map;
	}
	
	@RequestMapping(value = "/exportChargingRecord")
	@ResponseBody
	public Map<String, Object> exportChargingRecord(@RequestParam(value="checkFlag", required=false) String checkFlag, String lineName, HttpServletResponse response) {

		Map<String, Object> map = new HashMap<String, Object>();
		JSONArray jsonArray = null;
		try {
			jsonArray = Tools.readJsonFile(Const.CHARGINFRECORD_EXPORTJSONPATH, false);
			if (jsonArray == null) {
				map.put("result", "JsonError");
			} else {
				if (checkFlag != null && !checkFlag.isEmpty()) {
					logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录导出");
					try{
						ChargingRecord chargingRecord = (ChargingRecord) this.getSession().getAttribute("searchChargingRecord");
						if (chargingRecord != null) {
							List<ChargingRecord> chargingRecordList = reviewRecordService.getLineRecordDetail(chargingRecord);
							for (ChargingRecord chargingRecordInfo : chargingRecordList) {
								chargingRecordInfo.setTotalChargingTime(Tools.formatTime(chargingRecordInfo.getTotalChargingDTTM()));
							}
							map.put("chargingRecordList", chargingRecordList);
							ExcelUtil.exportExcel(lineName + Const.CHARGINFRECORD_EXCELNAME, jsonArray, map, response);
						}
					} catch (Exception e){
						logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录导出异常：" + e.getMessage());
					}
				} else {
					map.put("result", "Success");
				}
			}
		} catch (Exception e) {
			map.put("result", "JsonError");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录导出的JSON模板读取异常：" + e.getMessage());
		}
		return map;
	}
	
	@RequestMapping(value = "/searchUserInfo")
	@ResponseBody
	public Map<String, Object> searchUserInfo(LineInfo lineInfo){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "车队用户信息查询");
		List<UserInfo> list = null;
		try {
			list = userInfoService.getTeamUserInfo(lineInfo);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "车队用户信息查询异常" + e.getMessage());
			return map;
		}
		map.put("userInfo", list);
		list = null;
		return map;
	}
	
	@RequestMapping(value = "upLoadReviewFile")
	@ResponseBody
	public Map<String, Object> upLoadReviewFile(@RequestParam(value="importFile", required=false) MultipartFile files, String reviewDate, String chargingStationID, String lineID) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电量凭证文件上传");
		String filePath = null;
		File file = null;
		filePath = Const.BALANCE_PATH.concat("reviewfile");
		String [] reviewDateArr = reviewDate.split("[^\\d]");
		filePath = filePath.concat(File.separator).concat(reviewDateArr[0]).concat(File.separator).concat(reviewDateArr[1]).concat(File.separator).concat(reviewDateArr[2]).concat(File.separator).concat(chargingStationID);
		String reviewFileName = files.getOriginalFilename().split("\\.")[1];
		reviewFileName = lineID + "." + reviewFileName;
		if (files != null && !files.isEmpty()) {
			filePath = filePath + File.separator;
			if (reviewFileName != null && !reviewFileName.isEmpty()) {
				file = new File(filePath + reviewFileName);
				if (file.exists()) {
					map.put("result", "Exists");
					logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "上传电量凭证文件已存在！");
				} else {
					try {
						FileUpload.upload(files, filePath, reviewFileName, null);
						map.put("result", "Success");
						map.put("fileSize", files.getSize());
					} catch (Exception e) {
						map.put("result", "Error");
						logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电量凭证文件【" +  reviewFileName + "】上传异常：" + e.getMessage());
						e.printStackTrace();
					}
				}
			} else {
				map.put("result", "NameError");
			}
		} else {
			map.put("result", "NotFound");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电量凭证文件【" +  reviewFileName + "】不存在！");
		}
		return map;
	}
	
	@RequestMapping(value = "downLoadReviewFile")
	@ResponseBody
	public Map<String, Object> downLoadReviewFile(@RequestParam(value="fileName", required=false) String fileName, @RequestParam(value="reviewDate", required=false) String reviewDate, @RequestParam(value="chargingStationID", required=false) String chargingStationID,
			@RequestParam(value="checkFlag", required=false) String checkFlag, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		if(fileName != null && !fileName.isEmpty() && reviewDate != null && !reviewDate.isEmpty() && chargingStationID != null && !chargingStationID.isEmpty()){
			String[] reviewDateArr = reviewDate.split("[^\\d]");
			String path = Const.BALANCE_PATH.concat("reviewfile").concat(File.separator).concat(reviewDateArr[0]).concat(File.separator).concat(reviewDateArr[1]).concat(File.separator).concat(reviewDateArr[2]).concat(File.separator).concat(chargingStationID).concat(File.separator);
			path = path + fileName;
			if (checkFlag != null && !checkFlag.isEmpty()) {
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价文件下载开始");
				try{
					FileDownload.fileDownload(response, path, fileName);
				} catch (Exception e){
					logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价文件下载异常" + e.getMessage());
				}
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价文件下载结束");
			} else {
				File file = new File(path);
				if (file.exists()) {
					map.put("result", "Success");
				} else {
					map.put("result", "NotFound");
				}
			}
		} else {
			map.put("result", "NotFound");
		}
		return map;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "importReviewRecord")
	@ResponseBody
	public Map<String, Object> importChargingRecord(@RequestParam(value="importExcle", required=false) MultipartFile file) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "手抄记录导入");
		if (file != null && !file.isEmpty()) {
			try {
				List<ReviewRecord> importReviewRecordList = (List<ReviewRecord>) ExcelUtil.importExcel(file.getInputStream(), "/static/json/reviewRecordImportExcel.json", ReviewRecord.class);
				if (importReviewRecordList == null) {
					map.put("result", "JsonError");
					logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "导入失败，导入的JSON模板错误 或 不存在！");
				} else {
					int length = importReviewRecordList.size();
					if (length > 0) {
						int i = 0;
						boolean bool = true;
						for (ReviewRecord reviewRecordInfo : importReviewRecordList) {
							i++;
							reviewRecordInfo.setReviewPerson(this.getUesrInfo().getUserName());
							reviewRecordInfo.setReviewState(2);
							reviewRecordInfo.setReviewWay("entering");
							if(reviewRecordInfo.getReviewDate() == null) {
								map.put("result", "ReviewDateNull");
								map.put("index", i);
								bool = false;
								break;
							}
							if(reviewRecordInfo.getChargingStationName() == null) {
								map.put("result", "ChargingStationNull");
								map.put("index", i);
								bool = false;
								break;
							}
							if(reviewRecordInfo.getSubBranchCompanyName() == null) {
								map.put("result", "SubBranchCompanyNull");
								map.put("index", i);
								bool = false;
								break;
							}
							if(reviewRecordInfo.getTeamName() == null) {
								map.put("result", "TeamNameNull");
								map.put("index", i);
								bool = false;
								break;
							}
							if(reviewRecordInfo.getLineName() == null) {
								map.put("result", "LineNameNull");
								map.put("index", i);
								bool = false;
								break;
							}
							if(reviewRecordInfo.getReviewDate().length() > 10) {
								reviewRecordInfo.setReviewDate(reviewRecordInfo.getReviewDate().substring(0, 10));
							}
						}
						if(bool) {
							int count = reviewRecordService.saveReviewRecord(importReviewRecordList, this.getUesrInfo().getUserID());
							map.put("result", "Success");
							map.put("count", count - length);
						}
					} else {
						map.put("result", "NotFound");
						logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "导入失败，文件内没有可导入数据!");
					}
				}

			} catch (DataIntegrityViolationException e) {
				String[] error = e.getCause().getMessage().split("'");
				if("ChargingStationID".equals(error[1].trim())) {
					map.put("result", "ChargingStationIDNull");
				} else if("SubBranchCompanyID".equals(error[1].trim())) {
					map.put("result", "SubBranchCompanyIDNull");
				} else if("TeamID".equals(error[1].trim())) {
					map.put("result", "TeamIDNull");
				} else if("LineID".equals(error[1].trim())) {
					map.put("result", "LineIDNull");
				}
			} catch (Exception e) {
				map.put("result", "Error");
				logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "手抄记录文件【" +  file.getOriginalFilename() + "】导入异常：" + e.getMessage());
				e.printStackTrace();
			}
		} else {
			map.put("result", "NotFound");
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "导入失败，导入文件不存在!");
		}
		return map;
	}
	
	@RequestMapping(value = "exportReviewRecord")
	@ResponseBody
	public Map<String, Object> exportReviewRecord(@RequestParam(value="checkFlag", required=false) String checkFlag, HttpServletResponse response) {

		Map<String, Object> map = new HashMap<String, Object>();
		JSONArray jsonArray = null;
		try {
			jsonArray = Tools.readJsonFile("/static/json/reviewRecordExportExcel.json", false);
			if (jsonArray == null) {
				map.put("result", "JsonError");
			} else {
				if (checkFlag != null && !checkFlag.isEmpty()) {
					logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核记录导出开始");
					try{
						ReviewRecord reviewRecord = (ReviewRecord) this.getSession().getAttribute("searchReviewRecord");
						if (reviewRecord != null) {
							List<ReviewRecord> reviewRecordList = reviewRecordService.getReviewedRecord(reviewRecord);
							map.put("reviewRecordList", reviewRecordList);
							ExcelUtil.exportExcel("手抄记录", jsonArray, map, response);
						}
					} catch (Exception e){
						logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核记录导出异常：" + e.getMessage());
					}
				} else {
					map.put("result", "Success");
				}
			}
		} catch (Exception e) {
			map.put("result", "JsonError");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "审核记录导出的JSON模板读取异常：" + e.getMessage());
		}
		return map;
	}
	
	@RequestMapping(value = "/searchPhaseSummary")
	@ResponseBody
	public Map<String, Object> searchPhaseSummary(ChargingRecord chargingRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "分公司阶段电量查询");
		List<ReviewRecord> list = null;
		try {
			list = reviewRecordService.searchPhaseSummary(chargingRecord);
			this.getSession().setAttribute("searchPhaseSummary", list);
			this.getSession().setAttribute("phaseSummaryCondition", chargingRecord);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "分公司阶段电量查询异常" + e.getMessage());
			return map;
		}
		map.put("phaseSummaryList", list);
		chargingRecord = null;
		return map;
	}
	
	//手抄电量统计
	@RequestMapping(value = "/searchEntryStatistics")
	@ResponseBody
	public Map<String, Object> searchEntryStatistics(ReviewRecord reviewRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "分公司阶段电量查询");
		List<ReviewRecord> list = null;
		try {
			list = reviewRecordService.searchEntryStatistics(reviewRecord);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "分公司阶段电量查询异常" + e.getMessage());
			return map;
		}
		map.put("entryStatisticsList", list);
		reviewRecord = null;
		return map;
	}
}